package com.roamer.javase.demo.algorithm;

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.LockSupport;

/**
 * @author roamer
 * @version v1.0
 * @date 2021/3/15 19:30
 */
public class Channels {

    public static void main(String[] args) {
        Iterator<String> strs = null;
        LinkedBlockingQueue queue = new LinkedBlockingQueue(10);
        while (strs.hasNext()) {
            queue.add(strs.next());
        }
        ExecutorService executorService = new ThreadPoolExecutor(10, 10, 0, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(10));
        List<Future<Map<String, Integer>>> futures = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            Future<Map<String, Integer>> future = executorService.submit(new Callable<Map<String, Integer>>() {
                @Override
                public Map<String, Integer> call() throws Exception {
                    Map<String, Integer> countMap = new HashMap<>();
                    String str;
                    while (queue.isEmpty()) {
                        str = strs.next();
                        Integer count = countMap.getOrDefault(str, 0);
                        countMap.put(str, count++);

                    }
                    return countMap;
                }
            });
            futures.add(future);
        }
    }

    static class Channel<E> {
        private static final Object[] DEFAULT_ELEMENT_DATA = {};
        private Object[] elementData;

        private volatile E data;

        private volatile int cIndex = -1;
        private volatile int pIndex = -1;

        public Channel() {
            elementData = DEFAULT_ELEMENT_DATA;
        }

        public Channel(int capacity) {
            if (capacity > 0) {
                elementData = new Object[capacity];
            } else if (capacity == 0) {
                elementData = DEFAULT_ELEMENT_DATA;
            } else {
                throw new IllegalArgumentException("Illegal Capacity: " + capacity);
            }
        }

        public E get() {
            return null;
        }

        public void set(E element) {
            if (elementData == DEFAULT_ELEMENT_DATA) {
                // 不等于空则阻塞
                if (data != null) {
                    LockSupport.park();
                }
                data = element;
            } else {
                if (cIndex - pIndex > 0) {

                }
            }

        }

    }
}
